<template>
  <a-modal
    title="编辑物料"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="编码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入编码" v-decorator="['code', {rules: [{required: true, message: '请输入编码！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入名称" v-decorator="['name', {rules: [{required: true, message: '请输入名称！'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="型号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入型号" v-decorator="['model', {rules: [{required: false, message: '请输入型号！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="品牌"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select style="width: 100%" placeholder="请选择品牌" v-decorator="['brand', {rules: [{ required: false, message: '请选择品牌' }]}]">
                <a-select-option v-for="(item,index) in brandData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select style="width: 100%" placeholder="请选择类型" v-decorator="['type', {rules: [{ required: false, message: '请选择类型' }]}]">
                <a-select-option v-for="(item,index) in typeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="分类"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-tree-select
                v-decorator="['category', {rules: [{ required: false, message: '请选择分类' }]}]"
                style="width: 100%"
                :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
                :treeData="categoryTree"
                placeholder="请选择分类"
                treeDefaultExpandAll
              >
                <span slot="title" slot-scope="{ id }">{{ id }}
                </span>
              </a-tree-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="单位"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select style="width: 100%" placeholder="请选择单位" v-decorator="['unit', {rules: [{ required: true, message: '请选择单位！' }]}]">
                <a-select-option v-for="(item,index) in unitData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="管理类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select style="width: 100%" placeholder="请选择管理类型" v-decorator="['manageType', {rules: [{ required: true, message: '请选择管理类型！' }]}]">
                <a-select-option v-for="(item,index) in manageTypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="安全库存"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input-number placeholder="请输入安全库存" style="width: 100%" v-decorator="['safeStock', { initialValue: 0.00 }]" :min="0.00" :precision="2"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="是否启用"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select style="width: 100%" placeholder="请选择是否启用" v-decorator="['isEnable', {rules: [{ required: true, message: '请选择是否启用！' }]}]">
                <a-select-option v-for="(item,index) in isEnableData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="备注"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-textarea placeholder="请输入备注" v-decorator="['remark']" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { materialEdit } from '@/api/modular/main/base/material/materialManage'
  import { getCategoryTree } from '@/api/modular/main/base/material/materialCategoryManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        brandData: [],
        typeData: [],
        categoryTree: [],
        isEnableData: [],
        manageTypeData: [],
        unitData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true
        const brandOption = this.$options
        this.brandData = brandOption.filters['dictData']('material_brand')
        const typeOption = this.$options
        this.typeData = typeOption.filters['dictData']('material_type')
        const manageTypeOption = this.$options
        this.manageTypeData = manageTypeOption.filters['dictData']('material_manage_type')
        const isEnableOption = this.$options
        this.isEnableData = isEnableOption.filters['dictData']('whether')
        const unitOption = this.$options
        this.unitData = unitOption.filters['dictData']('material_unit')
        this.getCategoryTree()
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: this.$praseStrEmpty(record.id),
              code: this.$praseStrEmpty(record.code),
              name: this.$praseStrEmpty(record.name),
              brand: this.$praseStrEmpty(record.brand),
              type: this.$praseStrEmpty(record.type),
              category: this.$praseStrEmpty(record.category),
              model: this.$praseStrEmpty(record.model),
              unit: this.$praseStrEmpty(record.unit),
              manageType: this.$praseStrEmpty(record.manageType),
              safeStock: this.$praseStrEmpty(record.safeStock),
              isEnable: this.$praseStrEmpty(record.isEnable) + '',
              remark: this.$praseStrEmpty(record.remark)
            }
          )
        }, 100)
      },
            /**
       * 获取分类树，并加载于表单中
       */
      getCategoryTree () {
        getCategoryTree().then((res) => {
          this.formLoading = false
          if (!res.success) {
            this.categoryTree = []
            return
          }
          this.categoryTree = res.data
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            materialEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败:' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
